package app.util.image.calc
{
	public class MathUtility
	{
		public static function calcSummary(arr:Array, startIndex:int, endIndex:int = -1):Number
		{
			if(endIndex == -1)
			{
				endIndex = arr.length - 1;
			}
			
			var sum:Number = 0;
			for(var i:int = startIndex; i <= endIndex; i++)
			{
				var n:Number = arr[i] as Number;
				sum += n;
			}
			
			return sum;
		}
		
		public static function calcMedian(arr:Array):Number
		{
			var len:int = arr.length;
			arr.sort(Array.NUMERIC);
			return arr[len >> 1];
		}
		
		public static function clamp(min:Number, max:Number, v:Number):Number
		{
			if(v < min) return min;
			if(v > max) return max;
			return v;
		}
		
		public static function getMax(arr:Array):Number
		{
			var len:int = arr.length;
			var max:Number = arr[0];
			for(var i:int = 1; i < len; i++)
			{
				if(arr[i] > max)
				{
					max = arr[i];
				}
			}
			
			return max;
		}

		public static function getMin(arr:Array):Number
		{
			var len:int = arr.length;
			var min:Number = arr[0];
			for(var i:int = 1; i < len; i++)
			{
				if(arr[i] < min)
				{
					min = arr[i];
				}
			}
			
			return min;
		}
	}
}